package simplehdfs;


import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;


import java.io.*;
import java.net.URI;

/**
* @Author : liqiang
* @Date : 2020/12/15 20:25
* @Description : 将本地文件系统的文件通过java-api写入到hdfs文件
**/
public class CopyFileFromLocal {

    /**
     * @param args
     * arg0 windows本地磁盘文件C://test.txt
     * arg1 hadoop集群上的文件地址hdfs://node01:8020/test.txt
     * **/
    public static void main(String[] args){
        //本地磁盘路径
        String source = "E://score.txt";

        String destination = "hdfs://node01:8020/score.txt"; //HDF路径
        InputStream in = null;
        try{
            in = new BufferedInputStream(new FileInputStream(source));
            //HDFS读写的配置文件
            Configuration conf = new Configuration();

            FileSystem fs = FileSystem.get(URI.create(destination),conf);

            //调用create()返回的是FSDataOutputStream对象
            OutputStream out = fs.create(new Path(destination));
            IOUtils.copyBytes(in,out,4096,true);
        }catch (IOException e){
            e.printStackTrace();
        }
    }

}
